// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchVal: '',
    hotList: [],
    recommendList: [],
    dataList: [],
    showResultList: false,
    historicalList: [],

    isDel: false,
    isDark:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let storageDark = wx.getStorageSync('storageDark') ? wx.getStorageSync('storageDark') : false
    this.setData({
      isDark: storageDark
    })

    this.getHotList()

    let arr = wx.getStorageSync('historicalSearch') ? wx.getStorageSync('historicalSearch') : []
    this.setData({
      historicalList: arr
    })
    console.log(this.data.historicalList);
  },

  getHotList() {
    wx.request({
      url: 'https://app.kujiang.com/v1/book/get_all_search_words',
      success: ({ data }) => {
        this.setData({
          hotList: data.body
        })
      }
    })
  },

  getRecommendedSearchList(e) {

    this.setData({
      searchVal: e.detail
    })
    if (!e.detail.trim()) {
      this.setData({
        showResultList: false
      })
      return
    }

    wx.request({
      url: `https://app.kujiang.com/v1/book/pre_search202107?keyword=${this.data.searchVal}`,
      success: ({ data }) => {
        this.setData({
          recommendList: data.body.list
        })
        console.log(this.data.recommendList);

      }
    })
  },



  onSearch(e) {
    let kw = ''
    if (e.target.dataset.text) {
      kw = e.target.dataset.text
    } else {
      kw = e.detail
    }

    this.setData({
      searchVal: kw,
      showResultList: true
    })


    let arr = wx.getStorageSync('historicalSearch') ? wx.getStorageSync('historicalSearch') : []
    arr = arr.filter(t => t.kw != kw)
    arr.unshift({
      kw,
      id: Date.now()
    })
    this.setData({
      historicalList: arr,
      dataList: []

    })

    wx.setStorageSync('historicalSearch', arr)

    wx.request({
      url: `https://app.kujiang.com/v1/book/search202107?keyword=${kw}`,
      success: ({ data }) => {
        this.setData({
          dataList: data.body.list
        })
        console.log(this.data.dataList);

      }
    })

  },

  onCancel() {
    wx.navigateBack(-1)
  },

  onFocus() {
    this.setData({
      showResultList: false
    })

    wx.request({
      url: `https://app.kujiang.com/v1/book/pre_search202107?keyword=${this.data.searchVal}`,
      success: ({ data }) => {
        this.setData({
          recommendList: data.body.list
        })
      }
    })


  },

  removeFn(e) {

    let arr = wx.getStorageSync('historicalSearch') ? wx.getStorageSync('historicalSearch') : []

    arr = arr.filter(t => t.kw != e.target.dataset.text)
    this.setData({
      historicalList: arr
    })
  },

  changeIsDel() {
    this.setData({
      isDel: !this.data.isDel
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})